<template>
	<view>
		<view class="tupian">
			<view>
				<text class="text1">请上传歌单的封面</text>
			</view>
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10"
				height="300rpx" width="300rpx" uploadIconColor="#000000" style="background-color: #f0f0f0;">
			</u-upload>
		</view>
		<view class="wenzi">
			<view class="wenzitext">
				<text>歌单名称</text>
			</view>
			<u--input class="shurukuang" placeholder="请输入歌单名称" border="surround" v-model="listTittle" @change="change">
			</u--input>
		</view>
		<view class="wenzi">
			<view class="wenzitext">
				<text>歌单介绍</text>
			</view>
			<u--textarea class="shurukuang" v-model="listIntroduce" placeholder="请输入歌单介绍"></u--textarea>
		</view>
		<view class="wenzi">
			<view class="wenzitext">
				<text>歌单类型</text>
			</view>
			<view>
				<u-radio-group v-model="listStyle" iconPlacement="right" placement="column" borderBottom>
					<u-radio activeColor="blue" label="民谣" name="民谣"></u-radio>
					<u-radio activeColor="blue" label="轻音乐" name="轻音乐"></u-radio>
					<u-radio activeColor="blue" label="流行" name="流行"></u-radio>
					<u-radio activeColor="blue" label="说唱" name="说唱"></u-radio>
					<u-radio activeColor="blue" label="爵士" name="爵士"></u-radio>
					<u-radio activeColor="blue" label="摇滚" name="摇滚"></u-radio>
				</u-radio-group>
			</view>
			<button type="primary" class="bu" @tap="sub">添加表单</button>
			<view style="height: 50rpx;"></view>
		</view>
		
	</view>

</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
				listPhoto: "",
				listTittle: '',
				listIntroduce: '',
				listStyle: "",
				userId:""
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://124.221.244.228:8089/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
							let data = JSON.parse(res.data)
							that.listPhoto = data.data
						}
					});
				})
			},
			change(e) {
				console.log('change', e);
				console.log(this.value);
				console.log(this.listIntroduce)
			},
			async sub(){
				const res = await uni.request({
					url: "http://124.221.244.228:8089/list/insertList",
					method: 'POST',
					data: {
						listPhoto:this.listPhoto,
						listTittle:this.listTittle,
						listIntroduce:this.listIntroduce,
						listStyle:this.listStyle,
						userId:this.userId
					}
				})
				console.log(res);
				uni.navigateBack()
				uni.showToast({
					duration:500,
					title:"新建成功",
					icon:'success'
				})
			}
			
		},
		onLoad(index){
			this.userId = index.userId
		}

	}
</script>

<style lang="scss">
	.tupian {
		margin: 30rpx 40rpx;
	}

	.text1 {
		font-size: 35rpx;
		font-weight: 600;
		color: #5c5c5c
	}

	.wenzi {
		margin: 30rpx 40rpx;
	}

	.wenzitext {
		font-size: 35rpx;
		font-weight: 600;
		color: #5c5c5c;
		margin: 10rpx 0;
	}

	.shurukuang {
		margin: 10rpx 0;
	}

	.item {
		margin: 10rpx 0;
	}
	.bu{
		margin: 40rpx 30rpx;
	}
</style>
